<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        * {
            margin: 0;
            padding: 0;
        }

        html {
            text-align: center;
        }

        #rush {
            box-shadow: 0 0 10px #999;
        }

        #td {
            width: 300px;
            height: 180px;
            border: 1px solid orange;
            margin: 0 auto;
        }

        #tip {
            width: 100%;
            height: 100%;
        }
    </style>
</head>

<body>
    <div id="td">
        <canvas id="tip"></canvas>
    </div>
    <input type="text" id="rush" placeholder="验证码">
    <button id="btn">验证</button>
</body>
<script>

    var input = document.getElementsByTagName('input');

    var createNum = input.value;

    var numOne = 0;

    var str = "abcdefghijklmnopqrstuvwxyz";

    var data = str.toUpperCase();

    var num = "0123456789"

    var show = "" + data + str + num + "";

    console.log(show.length);


    function createRandomm(num1, num2) {
        var march = (num2 - num1)
        return randowNum = Math.floor(Math.random() * march) + num1
    }

    var vas = "";
    // 初始化值防止多次触发清空上个信息
    function martin() {
        vas = ""
        for (var i = 0; i < 5; i++) {
            vas += show[createRandomm(0, show.length - 1)]

        }

        console.log(vas);

        btn.onclick = function () {
            if (createNum == vas) {
                alert("验证码正确");
                numOne = 0;
            } else {
                martin();
                alert("验证码错误,请重新输入")
                numOne++;
            }
            if (numOne >= 3) {
                var a = 15
                time();
            }
            function time(){
             var cle =   setInterval(() => {
                    a--;
                    alert('失败多次，15s后请重新输入')
                }, 1000);
                setTimeout(() => {
                   clearInterval(cle); 
                   martin()
                }, 15000);
            }
            console.log("2333");
        }
        var ctx = tip.getContext("2d");
        ctx.clearRect(0, 0, 300, 180);
        //    画线段
        for (var i = 0; i < 99; i++) {
            ctx.strokeStyle = "rgba(" + createRandomm(0, 255) + "," + createRandomm(0, 255) + "," + createRandomm(0, 255) + "," + Math.random() + ")"
            ctx.beginPath();
            ctx.lineWidth = createRandomm(1, 3);
            ctx.moveTo(createRandomm(-30, 300), createRandomm(-30, 230));
            ctx.lineTo(createRandomm(-30, 300), createRandomm(-30, 230));
            ctx.stroke();
        }
        console.log("111");
        for (var i = 0; i < vas.length; i++) {

            ctx.font = createRandomm(30, 80) + "px 微软雅黑"
            // 随机颜色
            ctx.fillStyle = "rgba(" + createRandomm(0, 125) + "," + createRandomm(0, 125) + "," + createRandomm(0, 125) + "," + createRandomm(5, 10) / 10 + ")"
            // 上下居中
            ctx.textBaseline = "middle";

            ctx.fillText(vas[i], 50 * i + 40, 75);
        }
    }
    martin()
    td.onclick = martin;

</script>

</html>